﻿namespace FastColoredTextBoxNS
{
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Windows.Forms;

    [System.ComponentModel.ToolboxItem(false)]
    public class UnfocusablePanel : UserControl
    {
        public Color BackColor2 { get; set; }

        public Color BorderColor { get; set; }

        public new string Text { get; set; }

        public StringAlignment TextAlignment { get; set; }

        public UnfocusablePanel()
        {
            this.SetStyle(ControlStyles.Selectable, false);
            this.SetStyle(
                ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint, true);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            using (var brush = new LinearGradientBrush(this.ClientRectangle, this.BackColor2, this.BackColor, 90)) e.Graphics.FillRectangle(brush, 0, 0, this.ClientSize.Width - 1, this.ClientSize.Height - 1);
            using (var pen = new Pen(this.BorderColor)) e.Graphics.DrawRectangle(pen, 0, 0, this.ClientSize.Width - 1, this.ClientSize.Height - 1);

            if (!string.IsNullOrEmpty(this.Text))
            {
                var sf = new StringFormat();
                sf.Alignment = this.TextAlignment;
                sf.LineAlignment = StringAlignment.Center;
                using (var brush = new SolidBrush(this.ForeColor))
                    e.Graphics.DrawString(
                        this.Text,
                        this.Font,
                        brush,
                        new RectangleF(1, 1, this.ClientSize.Width - 2, this.ClientSize.Height - 2),
                        sf);
            }
        }
    }
}